home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / examples / chap06 / LionMovie.java < prev    next >
Text File  |  1996-09-27  |  3KB  |  95 lines

  1. //
  2. // lion is moving toward you.
  3. //
  4.  
  5. import vrml.*;
  6. import vrml.node.*;
  7. import vrml.field.*;
  8.  
  9. public class LionMovie extends Script{
  10.     SFTime startLion;
  11.     SFTime stopLion;
  12.     SFVec3f setLionPosition;
  13.     
  14.     // lion is moving or stopped.
  15.     boolean moving = false;
  16.  
  17.     // your position and lion position.
  18.     float userPosition[] = new float[3];
  19.     float lionPosition[] = new float[3];
  20.  
  21.     public void initialize(){
  22.         // get the reference of the event out 'startLion'.
  23.         startLion = (SFTime)getEventOut("startLion");
  24.         // get the reference of the event out 'stopLion'.
  25.         stopLion = (SFTime)getEventOut("stopLion");
  26.         // get the reference of the event out 'startLion'.
  27.         setLionPosition = (SFVec3f)getEventOut("setLionPosition");
  28.  
  29.         // initialize lion position
  30.         lionPosition[0] = 0.0f;
  31.         lionPosition[1] = 0.0f;
  32.         lionPosition[2] = 0.0f;
  33.     }
  34.  
  35.     public void processEvent(Event e){
  36.         if(e.getName().equals("touched") == true){
  37.             touched(e);
  38.         }else if(e.getName().equals("interval") == true){
  39.             interval();
  40.         }else if(e.getName().equals("getUserPosition") == true){
  41.             getUserPosition(e);
  42.         }
  43.     }
  44.  
  45.     // when you click the lion...
  46.     void touched(Event e){
  47.         double currentTime = ((ConstSFTime)e.getValue()).getValue();
  48.     
  49.         // toggle the lion state.
  50.         moving = !moving;
  51.  
  52.         // start or stop the lion.
  53.         if(true == moving){
  54.             startLion.setValue(currentTime);
  55.         }else{
  56.             stopLion.setValue(currentTime);
  57.         }
  58.     }
  59.  
  60.     // when the lion is moving toward you...
  61.     void interval(){
  62.         if(true == moving){
  63.             // move the lion toward you.
  64.  
  65.             float dx = userPosition[0] - lionPosition[0];
  66.             float dy = userPosition[1] - lionPosition[1];
  67.             float dz = userPosition[2] - lionPosition[2];
  68.             float distance = (float)Math.sqrt((double)(dx * dx +
  69.                                                        dy * dy +
  70.                                                        dz * dz));
  71.             if(distance < 3.0){
  72.                 // if the lion is near to you enough, do not move it anymore.
  73.                 return;
  74.             }
  75.                 
  76.             // relative movement distance is 1m.
  77.             dx = dx / distance;
  78.             dy = dy / distance;
  79.             dz = dz / distance;
  80.  
  81.             // update lion position.
  82.             lionPosition[0] += dx;
  83.             lionPosition[1] += dy;
  84.             lionPosition[2] += dz;
  85.             setLionPosition.setValue(lionPosition);
  86.         }
  87.     }
  88.  
  89.     // watch your current position.
  90.     void getUserPosition(Event e){
  91.         ((ConstSFVec3f)e.getValue()).getValue(userPosition);
  92.     }
  93. }
  94.  
  95.